﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using Follow.Utils;
using Follow.Model;
using System.Transactions;

namespace Follow.DataAccess
{
    public class VisitaDAO
    {
        private SqlDataReader reader = null;
        private SqlConnection conn = null;
        private SqlCommand cmd = null;

        public VisitaDAO()
        {
            conn = ConnectionUtils.getConnection();
        }

        public IList<Visita> findByFunc(Int32 id)
        {
            IList<Visita> listafunc = new List<Visita>();
            try
            {
                conn.Open();
                String sqlCommand = " select * from visita where funcionario_idfuncionario = @id;";
                cmd = new SqlCommand(sqlCommand, conn);
                cmd.Parameters.Add(new SqlParameter("id", id));
                reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    Visita v = new Visita();
                    v.IDFuncionarioVisita = reader.GetInt32(0);
                    listafunc.Add(v);

                }
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
                if (conn != null)
                {
                    conn.Close();
                }
            }
            return listafunc;
        }


        public IList<Visita> findByCli(Int32 id)
        {
            IList<Visita> listacliente = new List<Visita>();
            try
            {
                conn.Open();
                String sqlCommand = " select * from visita where cliente_idcliente = @id;";
                cmd = new SqlCommand(sqlCommand, conn);
                cmd.Parameters.Add(new SqlParameter("id", id));
                reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    Visita c = new Visita();
                    c.IDClienteVisita = reader.GetInt32(0);
                    listacliente.Add(c);

                }
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
                if (conn != null)
                {
                    conn.Close();
                }
            }
            return listacliente;
        }


        public IList<Visita> findAll()
        {
            IList<Visita> visita = new List<Visita>();

            try
            {
                conn.Open();

                String sqlCommand = " select visita.*, cliente.nome, funcionario.nome, visita.datavisita from visita, cliente, " +
                    " funcionario where visita.funcionario_idfuncionario = funcionario.idfuncionario and " +
                    " visita.cliente_idcliente = cliente.idcliente order by visita.datavisita;";
                cmd = new SqlCommand(sqlCommand, conn);
                reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    Visita vi = new Visita();
                    vi.IDVisita = reader.GetInt32(0);
                    vi.IDFuncionarioVisita = reader.GetInt32(1);
                    vi.IDClienteVisita = reader.GetInt32(2);
                    vi.DataVisita = reader.GetDateTime(3);
                    vi.ObsVisita = reader.GetString(4);
                    vi.NomeCliente = reader.GetString(5);
                    vi.NomeFuncionario = reader.GetString(6);
                    
                    visita.Add(vi);
                }
            }

            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }

                if (conn != null)
                {
                    conn.Close();
                }
            }

            return visita;
        }

        public void insert(Visita v, int idUsuario)
        {
            using (var transaction = new TransactionScope())
            {
                try
                {
                    conn.Open();
                    String sqlCommand = " insert into visita (funcionario_idfuncionario, cliente_idcliente, datavisita," +
                                        " observacoes) values (@idfunc, @idcliente, @datavisita, @obs);";                              
                    cmd = new SqlCommand(sqlCommand, conn);
                    cmd.Parameters.Add(new SqlParameter("idfunc", v.IDFuncionarioVisita));
                    cmd.Parameters.Add(new SqlParameter("idcliente", v.IDClienteVisita));
                    cmd.Parameters.Add(new SqlParameter("datavisita", v.DataVisita));
                    cmd.Parameters.Add(new SqlParameter("obs", v.ObsVisita));
                    cmd.ExecuteNonQuery();
                    transaction.Complete();
                }
                catch (Exception)
                {
                    transaction.Dispose();
                    throw;
                }
                finally
                {
                    if (reader != null)
                    {
                        reader.Close();
                    }

                    if (conn != null)
                    {
                        conn.Close();
                    }
                }
            }
            LogDAO log = new LogDAO();
            log.insert(idUsuario, Common.SQLStringSingleton.GetCommandText(cmd), "Insert", "Visitas");
        }

        public void update(Visita v, int idUsuario)
        {
            using (var transaction = new TransactionScope())
            {
                try
                {
                    conn.Open();
                    String sqlCommand = " update visita set funcionario_idfuncionario = @idfunc, cliente_idcliente = @idcliente," +
                                        " datavisita = @datavisita, observacoes = @obs where idvisita = @id;";
                    cmd = new SqlCommand(sqlCommand, conn);
                    cmd.Parameters.Add(new SqlParameter("id", v.IDVisita));
                    cmd.Parameters.Add(new SqlParameter("idfunc", v.IDFuncionarioVisita));
                    cmd.Parameters.Add(new SqlParameter("idcliente", v.IDClienteVisita));
                    cmd.Parameters.Add(new SqlParameter("datavisita", v.DataVisita));
                    cmd.Parameters.Add(new SqlParameter("obs", v.ObsVisita));
                    cmd.ExecuteNonQuery();
                    transaction.Complete();
                }
                catch (Exception)
                {
                    transaction.Dispose();
                    throw;
                }
                finally
                {
                    if (reader != null)
                    {
                        reader.Close();
                    }

                    if (conn != null)
                    {
                        conn.Close();
                    }
                }
            }
            LogDAO log = new LogDAO();
            log.insert(idUsuario, Common.SQLStringSingleton.GetCommandText(cmd), "Update", "Visitas");
        }

        public void delete(Int32 id, int idUsuario)
        {
            using (var transaction = new TransactionScope())
            {
                try
                {
                    conn.Open();
                    String sqlCommand = " delete from visita where idvisita = @id;";
                    cmd = new SqlCommand(sqlCommand, conn);
                    cmd.Parameters.Add(new SqlParameter("id", id));
                    cmd.ExecuteNonQuery();
                    transaction.Complete();
                }
                catch (Exception)
                {
                    transaction.Dispose();
                    throw;
                }
                finally
                {
                    if (reader != null)
                    {
                        reader.Close();
                    }

                    if (conn != null)
                    {
                        conn.Close();
                    }
                }
            }
            LogDAO log = new LogDAO();
            log.insert(idUsuario, Common.SQLStringSingleton.GetCommandText(cmd), "Delete", "Visitas");
        }

        public Visita findByID(Int32 id)
        {
            Visita visita = null;
            try
            {
                conn.Open();
                String sqlCommand = " select * from visita where idvisita = @id order by datavisita;";

                cmd = new SqlCommand(sqlCommand, conn);
                cmd.Parameters.Add(new SqlParameter("id", id));
                reader = cmd.ExecuteReader();

                if (reader.Read())
                {
                    visita = new Visita();
                    visita.IDVisita = reader.GetInt32(0);
                    visita.IDFuncionarioVisita = reader.GetInt32(1);
                    visita.IDClienteVisita = reader.GetInt32(2);
                    visita.DataVisita = reader.GetDateTime(3);
                    visita.ObsVisita = reader.GetString(4);
                }

            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
                if (conn != null)
                {
                    conn.Close();
                }
            }
            return visita;
        }

        public Cliente findByIdCliente(Int32 id)
        {
            Cliente cliente = null;
            try
            {
                conn.Open();
                String sqlCommand = " select * from visita where cliente_idcliente = " + id;
                cmd = new SqlCommand(sqlCommand, conn);
                reader = cmd.ExecuteReader();

                if (reader.Read())
                {
                    cliente = new Cliente();
                    cliente.IDCliente = reader.GetInt32(0);
                }

            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
                if (conn != null)
                {
                    conn.Close();
                }
            }
            return cliente;
        }

        public Visita findByDataVisita(DateTime datavisita)
        {
            Visita visita = null;
            try
            {
                conn.Open();
                String sqlCommand = " select * from visita where datavisita = '" + datavisita + "' order by datavisita";
                cmd = new SqlCommand(sqlCommand, conn);
                reader = cmd.ExecuteReader();

                if (reader.Read())
                {
                    visita = new Visita();
                    visita.IDVisita = reader.GetInt32(0);
                    visita.IDFuncionarioVisita = reader.GetInt32(1);
                    visita.IDClienteVisita = reader.GetInt32(2);
                    visita.DataVisita = reader.GetDateTime(3);
                    visita.ObsVisita = reader.GetString(4);
                }

            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
                if (conn != null)
                {
                    conn.Close();
                }
            }
            return visita;
        }

        public IList<Visita> findByNomeFuncionario(string Nome)
        {
            IList<Visita> visita = new List<Visita>();

            try
            {
                conn.Open();

                String sqlCommand = " select visita.*, cliente.nome, funcionario.nome, visita.datavisita from visita, cliente, " +
                    " funcionario where visita.funcionario_idfuncionario = funcionario.idfuncionario and " +
                    " visita.cliente_idcliente = cliente.idcliente and funcionario.nome like '%" + Nome + "%' order by visita.datavisita;";
                cmd = new SqlCommand(sqlCommand, conn);
                reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    Visita vi = new Visita();
                    vi.IDVisita = reader.GetInt32(0);
                    vi.IDFuncionarioVisita = reader.GetInt32(1);
                    vi.IDClienteVisita = reader.GetInt32(2);
                    vi.DataVisita = reader.GetDateTime(3);
                    vi.ObsVisita = reader.GetString(4);
                    vi.NomeCliente = reader.GetString(5);
                    vi.NomeFuncionario = reader.GetString(6);

                    visita.Add(vi);
                }
            }

            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }

                if (conn != null)
                {
                    conn.Close();
                }
            }

            return visita;
        }

      }


}
